import arcade

SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480

class Ball:

    def __init__(self, x, y, vx, vy, radius, color):
        self.x = x		# 球的x坐标
        self.y = y		# 球的y坐标
        self.vx = vx	 # 球x方向上的速度
        self.vy = vy	 # 球y方向上的速度
        self.radius = radius	 # 球的半径
        self.color = color		# 球的颜色

    def draw(self):
        arcade.draw_circle_filled(self.x, self.y, self.radius, self.color)

    def update(self):
        self.x += self.vx
        self.y += self.vy

        # 碰到边界时反弹
        if self.x < self.radius or self.x > SCREEN_WIDTH - self.radius:
            self.vx *= -1

        if self.y < self.radius or self.y > SCREEN_HEIGHT - self.radius:
            self.vy *= -1


class MyGame(arcade.Window):

    def __init__(self, width, height, title):
        super().__init__(width, height, title)

        arcade.set_background_color(arcade.color.ASH_GREY)

        self.ball_list = []

        ball = Ball(50, 50, 3, 3, 15, arcade.color.AUBURN)
        self.ball_list.append(ball)

        ball = Ball(150, 150, 2, 3, 15, arcade.color.PURPLE_MOUNTAIN_MAJESTY)
        self.ball_list.append(ball)

        ball = Ball(150, 250, -3, -1, 15, arcade.color.FOREST_GREEN)
        self.ball_list.append(ball)

    def on_draw(self):
        arcade.start_render()
        for ball in self.ball_list:
            ball.draw()


    def update(self, delta_time):
        for ball in self.ball_list:
            ball.update()

def main():
    window = MyGame(640, 480, "Example")

    arcade.run()

main()
